package web.servlet;

import domain.Account;
import domain.Item;
import domain.Product;
import service.CatalogService;
import service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ItemFormServlet extends HttpServlet {
    private CatalogService catalogService;

    private static final String PRODUCT_FORM = "/WEB-INF/jsp/catalog/product.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String productId = req.getParameter("productId");
        catalogService = new CatalogService();
        Product product = catalogService.getProduct(productId);
        HttpSession session = req.getSession();
        session.setAttribute("product",product);

        Account account=(Account)session.getAttribute("account");
        if(account != null){
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + req.getContextPath() + req.getServletPath() + "?" + (req.getQueryString());

            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + strBackUrl + " 查看宠物信息 " + productId;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        req.getRequestDispatcher(PRODUCT_FORM).forward(req,resp);
    }
}
